package com.secretsanta.generate;

import com.secretsanta.domain.User;
import com.secretsanta.service.UserService;

import java.util.*;

public class SantaAssigner {
    private UserService userService;
    private Random random = new Random();

    public void assign(Set<User> users) {
        Map<User, User> assignments = new HashMap<User, User>();
        List<User> alreadyUsed = new ArrayList<User>();
        for(User user: users) {
            Set<User> blacklist = user.getUserBlacklist();
            blacklist.addAll(alreadyUsed);
            List<User> potentialUsers = findUniqueValues(users, alreadyUsed);
            User selected = potentialUsers.get(random.nextInt(potentialUsers.size()));
            alreadyUsed.add(selected);
            user.setPresentReceiver(selected);
        }
    }

    private List<User> findUniqueValues(Set<User> input, List<User> toBeRemoved) {
        List<User> out = new ArrayList<User>();
        for(User user: input) {
            if(!toBeRemoved.contains(user)) {
                out.add(user);
            }
        }
        return out;
    }

}